package com.zjm.behavioral.command;

/**
 * 遥控器
 */
public class RemoteController {

    // 开 按钮命令数组
    Command[] onCommands;
    Command[] offCommands;

    // 撤销命令
    Command undoCommand;

    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];

        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    /**
     * 给按钮设置需要的命名
     *
     * @param no
     * @param onCommand
     * @param offCommand
     */
    public void setCommand(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    /**
     * 按下开按钮
     *
     * @param no
     */
    public void onButtonWasPushed(int no) {
        // 找到按下的开按钮，并执行方法
        onCommands[no].execute();
        // 记录操作用于撤销
        undoCommand = onCommands[no];
    }

    /**
     * 按下关按钮
     *
     * @param no
     */
    public void offButtonWasPushed(int no) {
        // 找到按下的关按钮，并执行方法
        offCommands[no].execute();
        // 记录操作用于撤销
        undoCommand = offCommands[no];
    }

    /**
     * 按下撤销按钮
     */
    public void undoButtonWasPushed() {
        undoCommand.undo();
    }
}
